package com.yidu.hbatsams.utils;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class NodeRole implements Serializable {


    /**
     *
     */
    private static final long serialVersionUID = 5805055138870807169L;
    /**
     *
     */
    private String pid;
    private String id; // 树的节点Id，区别于数据库中保存的数据Id。
    private String title;
    private boolean checked ;
    // 点击节点触发的链接
    private List<NodeRole> children = new ArrayList<NodeRole>(); // 子节点，可以用递归的方法读取

    /**
     * 生成一颗多叉树，根节点为root
     *
     * @param
     *
     * @return root
     */
    public List<NodeRole> createTree(List<NodeRole> nodes) {
        if (nodes == null || nodes.size() < 0)
            return null;
        List<NodeRole> nodeList = new ArrayList<>();// 根节点自定义，但是要和pid对应好
        // 将所有节点添加到多叉树中
        for (NodeRole node : nodes) {
            if (node.getPid().equals("0")) {// 根节点自定义，但是要和pid对应好
                // 向根添加一个节点
                nodeList.add(node);
            } else {
                addChild(nodes, node);
            }
        }
        return nodeList;
    }

    /**
     * 向指定多叉树节点添加子节点
     *
     * @param
     *
     * @param child
     *            节点
     */
    public void addChild(List<NodeRole> nodes, NodeRole child) {
        //System.out.println("nodes"+nodes);

        for (NodeRole item : nodes) {
            if (item.getId().equals(child.getPid())) {
                // 找到对应的父亲
                //System.out.println("2"+child);
                item.getChildren().add(child);
                break;
            }
        }
    }

    public NodeRole() {


    }








    @Override
    public String toString() {
        return "NodeRole [pid=" + pid + ", id=" + id + ", title=" + title + ", checked=" + checked + ", children="
                + children + "]";
    }



    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (checked ? 1231 : 1237);
        result = prime * result + ((children == null) ? 0 : children.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((pid == null) ? 0 : pid.hashCode());
        result = prime * result + ((title == null) ? 0 : title.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        NodeRole other = (NodeRole) obj;
        if (checked != other.checked)
            return false;
        if (children == null) {
            if (other.children != null)
                return false;
        } else if (!children.equals(other.children))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (pid == null) {
            if (other.pid != null)
                return false;
        } else if (!pid.equals(other.pid))
            return false;
        if (title == null) {
            if (other.title != null)
                return false;
        } else if (!title.equals(other.title))
            return false;
        return true;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<NodeRole> getChildren() {
        return children;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }



    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void setChildren(List<NodeRole> children) {
        this.children = children;
    }

    public NodeRole(String pid, String id, String title, boolean checked, List<NodeRole> children) {
        super();
        this.pid = pid;
        this.id = id;
        this.title = title;
        this.checked = checked;
        this.children = children;
    }




}

